using System;
using System.Collections.Generic;

namespace Common.Database
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class  DbTableSchema
    {
        #region fields
        private DataProviderType _DbProviderType;
        private string _ConnStr;
        private string _SchemaName;
        private string _TableName;
        private List<TableColumn> _Columns;
        #endregion

        #region ctor
        /// <summary></summary>
        public DbTableSchema()
        {
            this._DbProviderType = DataProviderType.MSSQL;
            this._ConnStr = "Server=(local);Database=Master;Integrated Security=true";
            this._SchemaName = string.Empty;
            this._TableName = "";
            this._Columns = new List<TableColumn>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tblName"></param>
        /// <param name="cols"></param>
        public DbTableSchema(DataProviderType dbProviderType, string connStr, string schemaName, string tblName, TableColumn[] cols)
        {
            this.DbProviderType = dbProviderType;
            this.ConnStr = connStr;
            this.SchemaName = schemaName;
            this.TableName = tblName;
            this.Columns = new List<TableColumn>();
            if(cols !=null && cols.Length>0)
            {
                for(int i=0;i<cols.Length;i++)
                {
                    this.Columns.Add(cols[i]);
                }
            }
        }
        #endregion

        #region props

        /// <summary></summary>
        public DataProviderType DbProviderType
        {
            get { return _DbProviderType; }
            set { _DbProviderType = value; }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get { return _ConnStr; }
            set { _ConnStr = value; }
        }

        /// <summary></summary>
        public string SchemaName
        {
            get { return _SchemaName; }
            set { _SchemaName = value; }
        }

        /// <summary></summary>
        public string TableName
        {
            get { return _TableName; }
            set { _TableName = value; }
        }

        /// <summary></summary>
        public List<TableColumn> Columns
        {
            get { return _Columns; }
            set { _Columns = value; }
        }
        #endregion

        #region static methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableKey"></param>
        /// <returns></returns>
        public static DbTableSchema GetTableSchema(DbTableKey tableKey)
        {
            TableColumn[] cols =
                DbUtil.GetTableColumns(tableKey.DbProvider, tableKey.ConnStr, tableKey.SchemaName, tableKey.TblName);
            return new DbTableSchema(tableKey.DbProvider, tableKey.ConnStr, tableKey.SchemaName, tableKey.TblName, cols);
        }
        #endregion
    }
}
